package com.googlecode.spreadit.activity;

import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.googlecode.spreadit.R;
import com.googlecode.spreadit.bean.SpreadRule;
import com.googlecode.spreadit.matching.SpreadRuleBase;

/**
 * This {@link ListActivity} relates to the {@link R.layout#manage_rules
 * "Manage Rules" screen}.
 * 
 * @author Markus L. Dechert (markus.dechert@googlemail.com)
 */
public class ManageRules extends ListActivity {

	private final String LOG_TAG = getClass().getSimpleName();

	public static final String HANDLER_ID = "com.googlecode.spreadit.activity.MANAGE_RULES";

	private ArrayAdapter<SpreadRule> rulesListAdapter;

	/** Called when the activity is first created. */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.manage_rules);

		final SpreadRuleBase srb = SpreadRuleBase.getInstance();
		rulesListAdapter = new ManageSpreadRuleArrayAdapter(this,
				srb.getRuleList());
		setListAdapter(rulesListAdapter);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		SpreadRule item = (SpreadRule) getListAdapter().getItem(position);

		Intent editRuleScreen = new Intent(EditRule.HANDLER_ID);
		editRuleScreen.putExtra(SpreadRule.ATTRIBUTE_ID, item.getId());
		startActivity(editRuleScreen);
	}

	@Override
	protected void onResume() {
		super.onResume();
		rulesListAdapter.notifyDataSetChanged();
	}

	/**
	 * This {@link ArrayAdapter} is used to render the {@link SpreadRule}s in
	 * the {@link R.layout#manage_rules "Manage Rules" screen}.
	 */
	class ManageSpreadRuleArrayAdapter extends ArrayAdapter<SpreadRule> {
		private final List<SpreadRule> items;
		private final LayoutInflater inflater;

		class ManageSpreadRuleViewHolder {
			public TextView textView;
			public ImageView imageView;

			public ManageSpreadRuleViewHolder(TextView textView, ImageView icon) {
				this.textView = textView;
				this.imageView = icon;
			}
		}

		public ManageSpreadRuleArrayAdapter(Context context,
				List<SpreadRule> arrayItems) {
			super(context, R.layout.list_item_manage_rules, arrayItems);
			this.items = arrayItems;
			this.inflater = (LayoutInflater) context
					.getSystemService(LAYOUT_INFLATER_SERVICE);
		}

		/**
		 * {@inheritDoc} Inflates the {@link View} for every {@link SpreadRule}
		 * in the list and fills it with data.
		 */
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			SpreadRule rule = items.get(position);

			ManageSpreadRuleViewHolder viewHolder;
			TextView tView;
			ImageView iView;

			/*
			 * no View exists yet, inflate one, find the views only once and
			 * save them in the views tag (next time, i.e. convertView!=null,
			 * the views are taken from the tag).
			 */
			if (v == null) {
				v = (View) inflater.inflate(R.layout.list_item_manage_rules,
						parent, false);
				tView = (TextView) v
						.findViewById(R.id.manage_rules_listItemCaption);
				iView = (ImageView) v
						.findViewById(R.id.manage_rules_listItemActive);
				viewHolder = new ManageSpreadRuleViewHolder(tView, iView);
				v.setTag(viewHolder);
			} else {
				viewHolder = (ManageSpreadRuleViewHolder) v.getTag();
				tView = viewHolder.textView;
				iView = viewHolder.imageView;
			}

			tView.setText(rule.getName());

			if (rule.isActive()) {
				iView.setBackgroundResource(R.drawable.turnedon);
			} else {
				iView.setBackgroundResource(R.drawable.turnedoff);
			}

			return v;
		}
	}

}
